//
//  YcKeyBoardView.m
//  KeyBoardAndTextView
//
//  Created by zzy on 14-5-28.
//  Copyright (c) 2014年 zzy. All rights reserved.
//

#import "YcKeyBoardView.h"

typedef void(^SendBlock)(NSString *content);

@interface YcKeyBoardView()<UITextViewDelegate>
@property (nonatomic,assign) CGFloat textViewWidth;
@property (nonatomic,assign) BOOL isChange;
@property (nonatomic,assign) BOOL reduce;
@property (nonatomic,assign) CGRect originalKey;
@property (nonatomic,assign) CGRect originalText;

@property (nonatomic ,strong) UIButton *sendButton;
@property (nonatomic ,copy) SendBlock sendBlock;

@end

@implementation YcKeyBoardView

- (instancetype)init {
    self = [super init];
    if (self) {
        
        [self addNotifition];
    }
    return self;
}

- (instancetype)initWithFrame:(CGRect)frame
//                     delegate:(id)delegate
                     complete:(void(^)(NSString *content))complete
{
//    self.delegate = delegate;
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        [self addNotifition];
        [self initTextView:frame];
    }
    self.sendBlock = complete;
    return self;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor=[UIColor whiteColor];
        [self initTextView:frame];
        [self addNotifition];
    }
    return self;
}

- (void)addNotifition {
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardHide:) name:UIKeyboardWillHideNotification object:nil];
}

-(void)initTextView:(CGRect)frame
{
    self.textView=[[UITextView alloc]init];
    self.textView.delegate=self;
    CGFloat textX=kStartLocation*0.5;
    self.textViewWidth=frame.size.width-2*textX;
    self.textView.frame=CGRectMake(textX, kStartLocation*0.2,self.textViewWidth - 60 , frame.size.height-2*kStartLocation*0.2);
    self.textView.backgroundColor=[UIColor colorWithRed:233.0/255 green:232.0/255 blue:250.0/255 alpha:1.0];
    self.textView.font=[UIFont systemFontOfSize:20.0];
    [self addSubview:self.textView];
    [self.textView becomeFirstResponder];
    self.textView.returnKeyType = UIReturnKeyNext;
    
    self.sendButton =[UIButton buttonWithType:UIButtonTypeRoundedRect];
    [self.sendButton setTitle:@"发送" forState:UIControlStateNormal];
    [self.sendButton addTarget:self action:@selector(forTalk) forControlEvents:UIControlEventTouchUpInside];
    self.sendButton.frame =CGRectMake(CGRectGetMaxX(self.textView.frame),5,40,CGRectGetHeight(self.textView.frame)-10);
    [self.sendButton setTintColor:[UIColor blackColor]];
    [self addSubview:self.sendButton];

    
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text{
    
//    if ([text isEqualToString:@"\n"]){
//        
//        if([self.delegate respondsToSelector:@selector(keyBoardViewHide: textView:)]){
//        
//            [self.delegate keyBoardViewHide:self textView:self.textView];
//        }
//        return NO;
//    }
    
    return YES;
}
-(void)textViewDidChange:(UITextView *)textView
{
    NSString *content=textView.text;
    CGSize contentSize=[content sizeWithAttributes:@{NSFontAttributeName:[UIFont systemFontOfSize:15]}];
    if(contentSize.width>self.textViewWidth){
          
      if(!self.isChange){
          
          CGRect keyFrame=self.frame;
          self.originalKey=keyFrame;
          keyFrame.size.height+=keyFrame.size.height;
          keyFrame.origin.y-=keyFrame.size.height*0.25;
          self.frame=keyFrame;
          
          CGRect textFrame=self.textView.frame;
          self.originalText=textFrame;
          textFrame.size.height+=textFrame.size.height*0.5+kStartLocation*0.2;
          self.textView.frame=textFrame;
          self.isChange=YES;
          self.reduce=YES;
          
          self.sendButton.frame =CGRectMake(CGRectGetMaxX(self.textView.frame),5,40,CGRectGetHeight(self.textView.frame)-10);
        }
    }
    
    if(contentSize.width<=self.textViewWidth){
        
        if(self.reduce){
            
            self.frame=self.originalKey;
            self.textView.frame=self.originalText;
            self.isChange=NO;
            self.reduce=NO;
            
            self.sendButton.frame =CGRectMake(CGRectGetMaxX(self.textView.frame),5,40,CGRectGetHeight(self.textView.frame)-10);
       }
    }
}

- (void)forTalk {
    self.sendBlock(self.textView.text);
}

-(void)keyboardShow:(NSNotification *)note
{
    CGRect keyBoardRect=[note.userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGFloat deltaY=keyBoardRect.size.height;
//    self.keyBoardHeight=deltaY;
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
        
        self.transform=CGAffineTransformMakeTranslation(0, -deltaY);
    }];
}
-(void)keyboardHide:(NSNotification *)note
{
    [UIView animateWithDuration:[note.userInfo[UIKeyboardAnimationDurationUserInfoKey] floatValue] animations:^{
        
        self.transform=CGAffineTransformIdentity;
    } completion:^(BOOL finished) {
        
        self.textView.text=@"";
        [self removeFromSuperview];
    }];
    
}

@end
